package com.promfy.async.simpleasynctest;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.promfy.R;

public class AsyncTaskActivity extends Activity {
	ProgressBar pb;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.asynctask);
        pb = (ProgressBar) findViewById(R.id.progressbar);
        
        new AsyncTaskTest().execute(0);
        
    }
    
    public class AsyncTaskTest extends AsyncTask<Integer, Integer, Integer> {

		@Override
		protected Integer doInBackground(Integer... arg0) {
			pb.setProgress(arg0[0]);
			int i;
			for(i=0; i<100; i++){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				publishProgress(i);
			}
			return i;
			
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			pb.setProgress(values[0]);
		}

		@Override
		protected void onPostExecute(Integer result) {
			if(result == 100){
				Toast.makeText(AsyncTaskActivity.this, "100", 1).show();
			}
		}
    	
    }
}